#!/usr/bin/env bash

# 阿拉伯数字18作为异常退出码
check='[ $? -eq 0 ] || exit 18'


eval "$check" && echo '进入 nutricula 目录'
eval "$check" && cd nutricula
eval "$check" && echo '进入虚拟环境'
# 虚拟环境下python和pip命令后不需要带3
eval "$check" && source venv/bin/activate
process_name='python manage.py runserver 0.0.0.0:14002'
process_cmd="ps ax|grep '$process_name'|grep -v grep|awk '{print $1}'"
old_pid=`eval $process_cmd`
echo "turritopsis old_pid=${old_pid}"
if [ -z "$old_pid" ];then
  echo "turritopsis 服务之前未重启过!"
else
  echo "turritopsis 关闭旧的进程!"
  kill -9 ${old_pid}
  mid_pid=`eval $process_cmd`
  if [ -z "${mid_pid}" ];then
      echo "turritopsis 进程关闭成功！"
  else
      echo "turritopsis 进程关闭失败，"
      exit 18
  fi
fi
eval "$check" && echo '启动服务turritopsis，允许所有IP访问'
eval "$check" && nohup $process_name >> turritopsis.log &

eval "$check" && echo 'finish turritopsis starting'